## Johnathan Cromwell & Hise Gibson
## 5/1/2013
## Gov2001 Replication Paper
## Extension of J.P. Eggers (2012) - Administrative Science Quarterly
## Falling Flat: Failed Technologies and Investment under Uncertainty

## In the original paper, Eggers finds that firms that made an early entry into LCD patents had signifiantly more patents related to LCD technology than late-entry firms.  He also finds that firms that invested solely in LCD technology do better than firms that hedged their bets by investing in both LCD and plasma technology.  

## In this paper, we want to take one of his control variables (ally) and use it as a dependent variable, asking the broad question of "What factors might lead to innovation partnerships?"  Given the dataset from the original Eggers paper, we believe we can reasonobly test four hypotheses. Note that we describe the dependent variable as the "sharing of knowledge and resources," which we find a more interesting question than "innovative alliance."  We believe that a co-patenting alliance is a good measure of the sharing of knowledge and resources, so the following hypotheses represent factors that might make firms more likely to share knowledge and resources:
## H1: Status of an innovator leads to more sharing of knowledge and resources.
## H2: Early entry into the LCD market leads to more sharing of knowledge and resources.
## H3: Japanese firms are more likely to share knowledge and resources than American firms.


## For the second portion of this analysis, we have decided to closely replicate the original Eggers paper, but modify the observation dataset to determine whether his conclusions are as broadly true as he claims.  Our deviation from his original paper comes in which observations to include.  He is looking for a firm's likelihood to produce more patents based on timing qualities of firm's investments.  To demonstrate this, he looks at patent data in which firms win patents in either LCD or plasma technology, which is so far so good.  However, many patents have multiple assignnees, meaning that multiple companies contribute to the production of the patent, however the way that this effort is distributed is not detailed in any way.  Eggers (2012) handles this by separating out each of the assignees for patents and attributing a 1-count for each company in the assignee list.  We find this to be short of satisfying because each of the observations is treated as independent and treated as evidence of the same effort and success.  In truth, the innovativeness of each company in the assignee list does not necessarily mean they became more expertly in the field, which means they would not necessarily gain more patents in the future.  There is some type of endogeity in the observations that we would like to eliminate.  We believe there is a potential opportunity to extend his analysis and his findings by showing boundary conditions related to whether or not companies allied.  To do this, we will perform the same analysis as his original paper, but include only observations in which companies did not ally.  This way, the observations are closer proxies for the same thing, which means they will support his argument further if our analysis provides the same results.  If our results differ, then we will provide a boundary condition on which his analysis and conclusions may not necessarily hold.  

  
## Import data
rm(list = ls())
library(foreign)
library(nlme)
library(MASS)
library(Hmisc)
data <- read.dta("/Users/jcromwell/Dropbox/Shared Folders/Gov2001 - Replication Paper/Patent Data/Patent Data 11213.dta")
pd.data <- as.data.frame(data)			

## Need to pare down the data so that there are only companies with more than one observation --> proxy for AR1 structure.  
ids <- as.matrix(unique(pd.data$id))
n <- length(ids)
b <- matrix(data=NA, nrow=n, ncol=1)
c <- matrix(data=NA, nrow=n, ncol=1)
for(i in 1:n){
	a <- subset(pd.data$id, pd.data$id == ids[i])
	b[i,] <- unique(a)
	c[i,] <- length(a)
	d <- cbind(b,c)				## d is a matrix of id #'s and their frequency
}

keep.ids <- subset(d, d[,2]>=2)[,1]
pd <- subset(pd.data, pd.data$id %in% keep.ids)

######################################################
## Table 1 - Summary Statistics
######################################################
## Select variables of interest
  lcd <- pd$lcd
  share_lcd <- pd$share_lcd
  enter_plasma <- pd$enter_plasma
  enter_both <- pd$enter_both
  enter_late <- pd$enter_late
  stock_lcd <- pd$stock_lcd
  stock_pdp <- pd$stock_pdp
  stock_general <- pd$stock_general
  ally <- pd$ally
  exper_pctvwatch <- pd$exper_pctvwatch
  japan <- pd$japan
  usa <- pd$usa
  europe <- pd$europe
  sales <- pd$sales
  sales_missing <- pd$sales_missing
  fe.year <- as.matrix(pd[,28:56])						## Year Fixed Effects: 1972-2000
  fe.id <- as.matrix(pd[,57:1116])					## Company Fixed Effects (x1064 companies)
   
  vars <- cbind(lcd, share_lcd, enter_plasma, enter_both, enter_late, stock_lcd, stock_pdp, stock_general, ally, exper_pctvwatch, japan, usa, europe, sales, sales_missing)
  
  ## Create the Correlation Matrix and Descriptive Statistics
  corr.matrix <- rcorr(vars)
  cor.matrix <- corr.matrix$r
  cor.matrix[upper.tri(cor.matrix, diag=T)] <- NA			## So that upper triangle reports NA
  
  vars.1 <- as.data.frame(vars)
  vars.1[,6] <- exp(stock_lcd)
  vars.1[,7] <- exp(stock_pdp)
  vars.1[,8] <- exp(stock_general)
  vars.1[,14] <- exp(sales)
  mean <- sapply(vars.1, mean, digits=4)
  sd <- sapply(vars.1, sd)
  min <- sapply(vars.1, min)
  max <- sapply(vars.1, max)
  
  ## Print the results in a table
  rownames <- c("1.LCD patents (DV)", "2.Share LCD at entry", "3.Enter with plasma", "4.Enter with both", "5.Late entrant dummy", "6.LCD patent stock", "7.Plasma patent stock", "8.General FPD patent stock", "9.Alliance or Partner", "10.PC/TV/Watch Experience", "11.Japanese focus", "12.American focus", "13.Western European focus", "14.Firm sales (t-1)", "15.Firm sales missing dummy", "Mean", "S.D.", "Min.", "Max.")
  colnames <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15")
  
  prep.table.1 <- as.data.frame(rbind(cor.matrix,mean,sd,min,max))
  Table.1 <- format(prep.table.1, digits = 2, justify = "none", scientific=F)
  rownames(Table.1) <- rownames
  colnames(Table.1) <- colnames
  write.csv(Table.1, "Table 1 - Correlation Table.csv")


######################################################
## Table 2 - Consequences of Technological Choices for Response to Emergent Design
######################################################
  
## Calculate results for Model 0
  ## Define values for Model 0
  x.0 <- cbind(stock_lcd, stock_pdp, stock_general, ally, exper_pctvwatch, japan, usa, europe, sales, sales_missing)    
  Model.0 <- summary(ml <- glm.nb(lcd ~ x.0 + fe.year, data=pd))
  M.0.betas <- as.matrix(Model.0$coefficients[1:11,1])
  M.0.stderr <- as.matrix(Model.0$coefficients[1:11,2])
  
## Calculate results for Model 1
  ## Define values for Model 1
  x.1 <- cbind(share_lcd, enter_late, stock_lcd, stock_pdp, stock_general, ally, exper_pctvwatch, japan, usa, europe, sales, sales_missing)
  Model.1 <- summary(ml <- glm.nb(lcd ~ x.1 + fe.year, data=pd))
  M.1.betas <- as.matrix(Model.1$coefficients[1:13,1])
  M.1.stderr <- as.matrix(Model.1$coefficients[1:13,2])

   
## Calculate results for Model 2  
  ## Define values for Model 2
  x.2 <- cbind(enter_plasma, enter_both, enter_late, stock_lcd, stock_pdp, stock_general, ally, exper_pctvwatch, japan, usa, europe, sales, sales_missing)
  Model.2 <- summary(ml <- glm.nb(lcd ~ x.2 + fe.year, data=pd))
  M.2.betas <- as.matrix(Model.2$coefficients[1:14,1])
  M.2.stderr <- as.matrix(Model.2$coefficients[1:14,2])
  
## Calculate results for Model 3  
  ## Interaction Terms
  plasma.general <- enter_plasma*stock_general
  plasma.experience <- enter_plasma*exper_pctvwatch
  both.general <- enter_both*stock_general
  both.experience <- enter_both*exper_pctvwatch
  
  x.3 <- cbind(enter_plasma, plasma.general, plasma.experience, enter_both, both.general, both.experience, enter_late, stock_lcd, stock_pdp, stock_general, ally, exper_pctvwatch, japan, usa, europe, sales, sales_missing)
  Model.3 <- summary(ml <- glm.nb(lcd ~ x.3 + fe.year, data=pd))
  M.3.betas <- as.matrix(Model.3$coefficients[1:18,1])
  M.3.stderr <- as.matrix(Model.3$coefficients[1:18,2])

  
  ## Print the results in a table
  ## List all results as columns to be binded  
  M.0.Table <- matrix(nrow=19, ncol=2)
  M.0.Table[9:18,1] <- M.0.betas[2:11,1]
  M.0.Table[9:18,2] <- M.0.stderr[2:11,1]
  M.0.Table[19,1] <- M.0.betas[1,1]
  M.0.Table[19,2] <- M.0.stderr[1,1]
  
  M.1.Table <- matrix(nrow=19, ncol=2)
  M.1.Table[1,1] <- M.1.betas[2,1]
  M.1.Table[1,2] <- M.1.stderr[2,1]
  M.1.Table[8,1] <- M.1.betas[3,1]
  M.1.Table[8,2] <- M.1.stderr[3,1]
  M.1.Table[9:18,1] <- M.1.betas[4:13,1]
  M.1.Table[9:18,2] <- M.1.stderr[4:13,1]
  M.1.Table[19,1] <- M.1.betas[1,1]
  M.1.Table[19,2] <- M.1.stderr[1,1]
  
  M.2.Table <- matrix(nrow=19, ncol=2)
  M.2.Table[2,1] <- M.2.betas[2,1]
  M.2.Table[2,2] <- M.2.stderr[2,1]
  M.2.Table[5,1] <- M.2.betas[3,1]
  M.2.Table[5,2] <- M.2.stderr[3,1]
  M.2.Table[8,1] <- M.2.betas[4,1]
  M.2.Table[8,2] <- M.2.stderr[4,1]
  M.2.Table[9:18,1] <- M.2.betas[5:14,1]
  M.2.Table[9:18,2] <- M.2.stderr[5:14,1]
  M.2.Table[19,1] <- M.2.betas[1,1]
  M.2.Table[19,2] <- M.2.stderr[1,1]
  
  M.3.Table <- matrix(nrow=19, ncol=2)
  M.3.Table[2:18,1] <- M.3.betas[2:18,1]
  M.3.Table[2:18,2] <- M.3.stderr[2:18,1]
  M.3.Table[19,1] <- M.3.betas[1,1]
  M.3.Table[19,2] <- M.3.stderr[1,1]


  ## Create labels for rows and columns
  rownames.2 <- as.matrix(c("Share LCD at entry","Enter with plasma","  xGeneral FPD patent stock", "  xPC/TV/watch Experience", "Enter with both","  xGeneral FPD patent stock.", "  xPC/TV/watch Experience.", "Late entrant dummy", "LCD patent stock", "Plasma patent stock", "General FPD patent stock", "Alliance or Partner", "PC/TV/Watch Experience", "Japanese focus", "American focus", "Western European focus", "Firm sales (t-1)", "Firm sales missing dummy", "Intercept"))
  colnames.2 <- as.matrix(c("Model 0", "Std Err", "Model 1", "Std Err", "Model 2", "Std Err", "Model 3", "Std Err"))
  
  prep.table.2 <- as.data.frame(cbind(M.0.Table, M.1.Table, M.2.Table, M.3.Table))
  Table.2 <- format(prep.table.2, digits = 2, justify = "none", scientific=F)
  rownames(Table.2) <- rownames.2
  colnames(Table.2) <- colnames.2
  write.csv(Table.2, "Table 2 - Models1:3.csv")


############################################################
############################################################
## Repeat analysis above with non-allied companies only
############################################################
############################################################

## Subset the previous data, keeping only companies that 
pd.ext <- subset(pd, pd$ally <=0.5)

######################################################
## Table 1 - Summary Statistics
######################################################
## Select variables of interest
  lcd.ext <- pd.ext$lcd
  share_lcd.ext <- pd.ext$share_lcd
  enter_plasma.ext <- pd.ext$enter_plasma
  enter_both.ext <- pd.ext$enter_both
  enter_late.ext <- pd.ext$enter_late
  stock_lcd.ext <- pd.ext$stock_lcd
  stock_pdp.ext <- pd.ext$stock_pdp
  stock_general.ext <- pd.ext$stock_general
  ally.ext <- pd.ext$ally
  exper_pctvwatch.ext <- pd.ext$exper_pctvwatch
  japan.ext <- pd.ext$japan
  usa.ext <- pd.ext$usa
  europe.ext <- pd.ext$europe
  sales.ext <- pd.ext$sales
  sales_missing.ext <- pd.ext$sales_missing
  fe.year.ext <- as.matrix(pd.ext[,28:56])				## Year Fixed Effects: 1972-2000
  fe.id.ext <- as.matrix(pd.ext[,57:1116])				## Company Fixed Effects (x1064 companies)
   
  vars.ext <- cbind(lcd.ext, share_lcd.ext, enter_plasma.ext, enter_both.ext, enter_late.ext, stock_lcd.ext, stock_pdp.ext, stock_general.ext, ally.ext, exper_pctvwatch.ext, japan.ext, usa.ext, europe.ext, sales.ext, sales_missing.ext)
  
  ## Create the Correlation Matrix and Descriptive Statistics
  corr.matrix.ext <- rcorr(vars.ext)
  cor.matrix.ext <- corr.matrix.ext$r
  cor.matrix.ext[upper.tri(cor.matrix.ext, diag=T)] <- NA			## So that upper triangle reports NA
  
  vars.1.ext <- as.data.frame(vars.ext)
  vars.1.ext[,6] <- exp(stock_lcd.ext)
  vars.1.ext[,7] <- exp(stock_pdp.ext)
  vars.1.ext[,8] <- exp(stock_general.ext)
  vars.1.ext[,14] <- exp(sales.ext)
  mean.ext <- sapply(vars.1.ext, mean, digits=4)
  sd.ext <- sapply(vars.1.ext, sd)
  min.ext <- sapply(vars.1.ext, min)
  max.ext <- sapply(vars.1.ext, max)
  
  ## Print the results in a table
  rownames.ext <- c("1.LCD patents (DV)", "2.Share LCD at entry", "3.Enter with plasma", "4.Enter with both", "5.Late entrant dummy", "6.LCD patent stock", "7.Plasma patent stock", "8.General FPD patent stock", "9.Alliance or Partner", "10.PC/TV/Watch Experience", "11.Japanese focus", "12.American focus", "13.Western European focus", "14.Firm sales (t-1)", "15.Firm sales missing dummy", "Mean", "S.D.", "Min.", "Max.")
  colnames.ext <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15")
  
  prep.table.1.ext <- as.data.frame(rbind(cor.matrix.ext,mean.ext,sd.ext,min.ext,max.ext))
  Table.1.ext <- format(prep.table.1.ext, digits = 2, justify = "none", scientific=F)
  rownames(Table.1.ext) <- rownames.ext
  colnames(Table.1.ext) <- colnames.ext
  write.csv(Table.1.ext, "Table 1 - Correlation Table (No Ally).csv")


######################################################
## Table 2 - Consequences of Technological Choices for Response to Emergent Design
######################################################
  
## Calculate results for Model 0
  ## Define values for Model 0
  x.0.ext <- cbind(stock_lcd.ext, stock_pdp.ext, stock_general.ext, ally.ext, exper_pctvwatch.ext, japan.ext, usa.ext, europe.ext, sales.ext, sales_missing.ext)    
  Model.0.ext <- summary(ml <- glm.nb(lcd.ext ~ x.0.ext + fe.year.ext, data=pd.ext))
  M.0.betas.ext <- as.matrix(Model.0.ext$coefficients[1:11,1])
  M.0.stderr.ext <- as.matrix(Model.0.ext$coefficients[1:11,2])
  
## Calculate results for Model 1
  ## Define values for Model 1
  x.1.ext <- cbind(share_lcd.ext, enter_late.ext, stock_lcd.ext, stock_pdp.ext, stock_general.ext, ally.ext, exper_pctvwatch.ext, japan.ext, usa.ext, europe.ext, sales.ext, sales_missing.ext)
  Model.1.ext <- summary(ml <- glm.nb(lcd.ext ~ x.1.ext + fe.year.ext, data=pd.ext))
  M.1.betas.ext <- as.matrix(Model.1.ext$coefficients[1:13,1])
  M.1.stderr.ext <- as.matrix(Model.1.ext$coefficients[1:13,2])

   
## Calculate results for Model 2  
  ## Define values for Model 2
  x.2.ext <- cbind(enter_plasma.ext, enter_both.ext, enter_late.ext, stock_lcd.ext, stock_pdp.ext, stock_general.ext, ally.ext, exper_pctvwatch.ext, japan.ext, usa.ext, europe.ext, sales.ext, sales_missing.ext)
  Model.2.ext <- summary(ml <- glm.nb(lcd.ext ~ x.2.ext + fe.year.ext, data=pd.ext))
  M.2.betas.ext <- as.matrix(Model.2.ext$coefficients[1:14,1])
  M.2.stderr.ext <- as.matrix(Model.2.ext$coefficients[1:14,2])
  
## Calculate results for Model 3  
  ## Interaction Terms
  plasma.general.ext <- enter_plasma.ext*stock_general.ext
  plasma.experience.ext <- enter_plasma.ext*exper_pctvwatch.ext
  both.general.ext <- enter_both.ext*stock_general.ext
  both.experience.ext <- enter_both.ext*exper_pctvwatch.ext
  
  x.3.ext <- cbind(enter_plasma.ext, plasma.general.ext, plasma.experience.ext, enter_both.ext, both.general.ext, both.experience.ext, enter_late.ext, stock_lcd.ext, stock_pdp.ext, stock_general.ext, ally.ext, exper_pctvwatch.ext, japan.ext, usa.ext, europe.ext, sales.ext, sales_missing.ext)
  Model.3.ext <- summary(ml <- glm.nb(lcd.ext ~ x.3.ext + fe.year.ext, data=pd.ext))
  M.3.betas.ext <- as.matrix(Model.3.ext$coefficients[1:18,1])
  M.3.stderr.ext <- as.matrix(Model.3.ext$coefficients[1:18,2])

  
  ## Print the results in a table
  ## List all results as columns to be binded  
  M.0.Table.ext <- matrix(nrow=19, ncol=2)
  M.0.Table.ext[9:18,1] <- M.0.betas.ext[2:11,1]
  M.0.Table.ext[9:18,2] <- M.0.stderr.ext[2:11,1]
  M.0.Table.ext[19,1] <- M.0.betas.ext[1,1]
  M.0.Table.ext[19,2] <- M.0.stderr.ext[1,1]
  
  M.1.Table.ext <- matrix(nrow=19, ncol=2)
  M.1.Table.ext[1,1] <- M.1.betas.ext[2,1]
  M.1.Table.ext[1,2] <- M.1.stderr.ext[2,1]
  M.1.Table.ext[8,1] <- M.1.betas.ext[3,1]
  M.1.Table.ext[8,2] <- M.1.stderr.ext[3,1]
  M.1.Table.ext[9:18,1] <- M.1.betas.ext[4:13,1]
  M.1.Table.ext[9:18,2] <- M.1.stderr.ext[4:13,1]
  M.1.Table.ext[19,1] <- M.1.betas.ext[1,1]
  M.1.Table.ext[19,2] <- M.1.stderr.ext[1,1]
  
  M.2.Table.ext <- matrix(nrow=19, ncol=2)
  M.2.Table.ext[2,1] <- M.2.betas.ext[2,1]
  M.2.Table.ext[2,2] <- M.2.stderr.ext[2,1]
  M.2.Table.ext[5,1] <- M.2.betas.ext[3,1]
  M.2.Table.ext[5,2] <- M.2.stderr.ext[3,1]
  M.2.Table.ext[8,1] <- M.2.betas.ext[4,1]
  M.2.Table.ext[8,2] <- M.2.stderr.ext[4,1]
  M.2.Table.ext[9:18,1] <- M.2.betas.ext[5:14,1]
  M.2.Table.ext[9:18,2] <- M.2.stderr.ext[5:14,1]
  M.2.Table.ext[19,1] <- M.2.betas.ext[1,1]
  M.2.Table.ext[19,2] <- M.2.stderr.ext[1,1]
  
  M.3.Table.ext <- matrix(nrow=19, ncol=2)
  M.3.Table.ext[2:18,1] <- M.3.betas.ext[2:18,1]
  M.3.Table.ext[2:18,2] <- M.3.stderr.ext[2:18,1]
  M.3.Table.ext[19,1] <- M.3.betas.ext[1,1]
  M.3.Table.ext[19,2] <- M.3.stderr.ext[1,1]


  ## Create labels for rows and columns
  rownames.2.ext <- as.matrix(c("Share LCD at entry","Enter with plasma","  xGeneral FPD patent stock", "  xPC/TV/watch Experience", "Enter with both","  xGeneral FPD patent stock.", "  xPC/TV/watch Experience.", "Late entrant dummy", "LCD patent stock", "Plasma patent stock", "General FPD patent stock", "Alliance or Partner", "PC/TV/Watch Experience", "Japanese focus", "American focus", "Western European focus", "Firm sales (t-1)", "Firm sales missing dummy", "Intercept"))
  colnames.2.ext <- as.matrix(c("Model 0", "Std Err", "Model 1", "Std Err", "Model 2", "Std Err", "Model 3", "Std Err"))
  
  prep.table.2.ext <- as.data.frame(cbind(M.0.Table.ext, M.1.Table.ext, M.2.Table.ext, M.3.Table.ext))
  Table.2.ext <- format(prep.table.2.ext, digits = 2, justify = "none", scientific=F)
  rownames(Table.2.ext) <- rownames.2.ext
  colnames(Table.2.ext) <- colnames.2.ext
  write.csv(Table.2.ext, "Table 2 - Models1:3 (No Ally).csv")

